// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import { getTimehms } from "../../GlobalFunction";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import ShopManager, { ShopEvent } from "../../Models/Shops/ShopManager";
import ShopTemplate from "../../Models/Shops/ShopTemplate";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ShopItem extends cc.Component {

    @property(cc.Label)
    nameTxt: cc.Label = null;
    @property(cc.Label)
    costTxt: cc.Label = null;
    @property(cc.Button)
    buyBtn: cc.Button = null;
    @property(cc.Label)
    limitTxt: cc.Label = null;
    @property(cc.Label)
    cdTxt: cc.Label = null;
    @property(cc.Sprite)
    icon: cc.Sprite = null;
    @property(cc.Button)
    seeVideoBtn: cc.Button = null;

    shopTp: ShopTemplate;
    leftTime: number = 0;

    public drawItem(tp: ShopTemplate): void {
        this.shopTp = tp;

        this.nameTxt.string = this.shopTp.shopName;
        this.costTxt.string = this.shopTp.costType == 1 ? "" : "" + this.shopTp.cost;
        var ey = ShopManager.single.getShopEntitiy(tp.id);
        if (ey != null) {

            this.limitTxt.string = "购买次数：" + ey.buyTimes + "/10";
            this.leftTime = ey.leftTime * 0.001;
            if (this.shopTp.costType > 0) {
                this.seeVideoBtn.node.active = false;
                if (this.leftTime > 0) {
                    this.buyBtn.node.active = false;
                    this.cdTxt.node.active = true;
                    this.cdTxt.string = getTimehms(this.leftTime);
                } else {
                    this.buyBtn.node.active = true;
                    this.cdTxt.node.active = false;
                    this.buyBtn.interactable = ey.buyTimes < 10;
                }
            } else {
                this.buyBtn.node.active = false;
                if (this.leftTime > 0) {
                    this.seeVideoBtn.node.active = false;
                    this.cdTxt.node.active = true;
                    this.cdTxt.string = getTimehms(this.leftTime);
                } else {
                    this.seeVideoBtn.node.active = true;
                    this.cdTxt.node.active = false;
                    this.seeVideoBtn.interactable = ey.buyTimes < 10;
                }
            }

        } else {
            this.buyBtn.node.active = this.shopTp.costType > 0;
            this.seeVideoBtn.node.active = this.shopTp.costType == 0
            this.cdTxt.node.active = false;
            this.limitTxt.string = "购买次数：0/10";

        }
        if (this.shopTp.icon != "0") {
            cc.resources.load("UIRes/ShopRes/" + this.shopTp.icon, cc.SpriteFrame, (error: Error, assets: cc.SpriteFrame) => {
                this.icon.spriteFrame = assets;
            });
        } else {
            this.icon.spriteFrame = null;
        }

    }

    buyClick() {
        if (this.shopTp.costType == 1 && !SaveManager.single.isEnoughGold(this.shopTp.cost)) {
            UIManager.single.showTips("金币不足。");
            return;
        }
        if (this.shopTp.costType == 2 && !SaveManager.single.isEnoughDiamond(this.shopTp.cost)) {
            UIManager.single.showTips("钻石不足。");
            return;
        }
        ShopManager.single.requestBuy(this.shopTp.id);
        GameEvent.eventNode.emit(ShopEvent.BUYGOODS_EVENT);
    }
    seeVideoClick() {
        var userdata = SaveManager.single.userdata;
        var ey = userdata.getShopEntity(this.shopTp.id);
        if (ey != null && ey.buyTimes >= 10) {
            UIManager.single.showTips("今天获得次数已满。");
            return;
        }
        var rewardStr = "";
        if (this.shopTp.id == 101) {
            rewardStr = "10000金币。";
        } else if (this.shopTp.id == 102) {
            rewardStr = "100钻石。";
        } else if (this.shopTp.id == 103) {
            rewardStr = "移速+50%，持续3分钟。";
        } else if (this.shopTp.id == 105) {
            rewardStr = "攻击+50%，持续3分钟。";
        }

        UIManager.single.showDialog("观看一段广告\n获得@1".replace("@1", rewardStr), () => {
            ShopManager.single.requestBuy(this.shopTp.id);
            GameEvent.eventNode.emit(ShopEvent.BUYGOODS_EVENT);
        }, null, this, true);
    }


    protected update(dt: number): void {
        if (this.leftTime > 0) {
            this.leftTime -= dt;
            this.cdTxt.string = getTimehms(this.leftTime);
        } else {
            this.cdTxt.string = "";
        }
    }

}
